﻿//Serializer class
namespace Compression.ImageCompressor
{
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO.Compression;
    using System.Text;

    public static class Serializer
    {
        private static byte[] buff = new byte[1024];

        /// <summary>
        /// Serialize Header
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        //public static string Serialize(object header)
        public static MemoryStream Serialize(object header)
        {
            StringBuilder build = new StringBuilder();
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream mem = new MemoryStream();
            //GZipStream zip = new GZipStream(mem, CompressionMode.Compress);
            
            bf.Serialize(mem, header);
            //bf.Serialize(zip, header);
            mem.Position = 0;
            //build.Append(ASCIIEncoding.Unicode.GetChars(mem.ToArray()));            
            //return build.ToString();
            
            return mem;
        }

        /// <summary>
        /// Deserialize Header
        /// </summary>
        /// <param name="code"></param>
        /// <param name="header"></param>
        /// <returns></returns>
        //public static void Deserialize(string code, out object header)
        public static void Deserialize(MemoryStream mem, out object header)
        {
            //MemoryStream mem = new MemoryStream(ASCIIEncoding.Unicode.GetBytes(code));                        
            //GZipStream zip = new GZipStream(mem, CompressionMode.Decompress);         
               
            //BinaryWriter writer = new BinaryWriter(mem);
            mem.Position = 0;
            BinaryFormatter bf = new BinaryFormatter();            
            header = bf.Deserialize(mem);
            //header = bf.Deserialize(zip);
            if (mem != null) mem.Dispose();
        }
    }
}
